package com.lyw.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyw.gulimall.product.entity.ProductAttrValueEntity;
import com.lyw.gulimall.product.service.ProductAttrValueService;
import com.lyw.gulimall.product.vo.AttrRespVo;
import com.lyw.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lyw.gulimall.product.service.AttrService;
import com.lyw.common.utils.R;



/**
 * 商品属性
 *
 * @author liyawei
 * @email liyawei@gmail.com
 * @date 2022-05-31 11:35:50
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    // /api/product/attr/update/14
    @PostMapping("/update/{spuId}")
    public R getAttrInfo(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> productAttrValueEntities) {
        productAttrValueService.updateSpuAttr(spuId, productAttrValueEntities);
        return R.ok();
    }


    // /product/attr/info/{attrId}
    /*@GetMapping("/info/{attrId}")
    public R getAttrInfo(@PathVariable("attrId") Long attrId) {

        return R.ok();
    }*/

    /**
     *
     * @param spuId
     * @return
     */
    // /product/attr/base/listforspu/{spuId}
    @GetMapping("/base/listforspu/{spuId}")
    public R getBaseAttrListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> list =productAttrValueService.getBaseAttrListForSpu(spuId);

        return R.ok().put("data", list);
    }

    // /product/attr/sale/list/0?t=1656216780851&page=1&limit=10&key=
    // /api/product/attr/base/list/0?t=1656148281483&page=1&limit=10&key=
    // attrType 根据前端传入的数据判断是基础属性还是销售属性接口
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                      @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                      @RequestParam(value = "key", required = false) String name,
                      @PathVariable(value = "catelogId", required = false) Long catelogId,
                      @PathVariable(value = "attrType") String attrType) {

        IPage<AttrRespVo> list = attrService.queryAttrBasePage(catelogId, page, limit, name, attrType);

        return R.ok().put("page", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                  @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                  @RequestParam(value = "key", required = false) String name){
        IPage<Map<String, Object>> list = attrService.queryPage(page, limit, name);

        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    // /api/product/attr/info/1?t=1656152750344
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId)

        AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
